package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.core.dao.database.IShopTokenDao;
import com.singfan.core.dao.database.model.ShopToken;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * Author: hansonghe
 * Date: 2015-10-08 15:49
 * Desc:
 */
@Repository
public class ShopTokenDaoImpl extends DaoBaseGeneric<ShopToken> implements IShopTokenDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public void insertOrUpdateToken(long shopId, String token, int cityId, int status) {
        ShopToken shopToken = new ShopToken();
        shopToken.setShopId(shopId);
        shopToken.setAccessToken(token);
        shopToken.setStatus(status);
        shopToken.setCityId(cityId);
        shopToken.setTokenCreateTime(new Date());

        replace(shopId, shopToken);
    }

    @Override
    public ShopToken findShopToken(String token) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE access_token=?";
        return selectOne(null, sql, token);
    }
}
